<?php
class Admin_RaidController extends Admin_BaseController {

	public function getRaids() {
		$raids = Raid::orderBy('raid_id', 'DESC')->paginate(20);
		return View::make("admin.raid.index", compact('raids'));
	}

	public function addRaid() {
		return View::make("admin.raid.add");
	}

	public function postAddRaid() {
		$rules = array(
			'instance' => 'required|between:1,300',
			'date' => 'required|date',
			'characters' => 'required'
		);

		$validator = Validator::make(Input::all(), $rules);

		if ($validator->fails())
        {
			return Redirect::back()->withInput()->withErrors($validator);	
        }
        else 
        {
        	$date = new DateTime(Input::get('date'));
        	$raid = new Raid;
        	$raid->instance = Input::get('instance');
        	$raid->date = $date->format("Y-m-d H:i:s");
        	$raid->user_id = Auth::user()->id;
        	$raid->save();

        	$characters = Input::get('characters');

        	foreach($characters as $char)
        	{
        		$raidmember = new RaidMember;
        		$raidmember->raid_id = $raid->raid_id;
        		$raidmember->name = $char;
        		$raidmember->save();
        	}

			return Redirect::route('admin-raids');
        }
	}

	public function asyncSearchCharacter() {
		$rules = array(
			'searchc' => 'required'
		);

		$validator = Validator::make(Input::all(), $rules);

		if(!$validator->fails())
		{
			$searchstring = Input::get('searchc');
			$characters = Characters::search($searchstring)->orderBy('char_name')->get();
			return View::make('admin.async.charactersearch', compact('characters', 'searchstring'));
		}
	}
}